package cn.dengta.webapp.user.vo;

import java.util.Date;

import cn.dengta.common.util.Formats;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.userLevel.UserLevelRule;
import cn.dengta.webapp.user.entity.userLevel.UserLevelRule.UserLevelRuleType;
import cn.dengta.webapp.user.model.*;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.X;

@Getter
@Setter
public class UserLevelRuleVO extends AbstractVO<UserLevelRule> {

	/** 名称 */
	private Object name;
	/** 图标 */
	private String icon;
	/** 任务类型 */
	private TaskType type;
	/** 业务类型 */
	private UserLevelRuleType ruleType;
	/** 最小经验值 */
	private Integer minExp;
	/** 领取最大次数 */
	private Integer maxLimit;
	protected Boolean completed;
	protected Integer completeCount;
	protected String url;
	/** 排序 */
	private Integer sort;
	/** 开始时间 */
	private transient Date beginTime;
	/** 结束时间 */
	private transient Date endTime;
	private transient ActivityStatusDescriptor statusDescriptor;
	/** 规则JSON配置 */
	private Object config;

	public static UserLevelRuleVO from(UserLevelRule source) {
		UserLevelRuleVO vo = copy(source, UserLevelRuleVO::new);
		if (vo != null) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::getDataMap);
			vo.config = source.getResolvedCfg();
			vo.statusDescriptor = source.getStatusDescriptor(vo.now().getTime());
		}
		return vo;
	}

	public static UserLevelRuleVO fromView(UserLevelRule source) {
		UserLevelRuleVO vo = copy(source, UserLevelRuleVO::new, "config", "sort", "statusDescriptor");
		if (vo != null) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::get);
		}
		return vo;
	}

	public Integer getRuleType() {
		return X.map(ruleType, UserLevelRuleType::getValue);
	}

	public String getRuleType_() {
		return X.map(ruleType, UserLevelRuleType::getLabel);
	}

	public String getType_() {
		return X.map(type, TaskType::getDesc);
	}

	public Integer getType() {
		return X.map(type, TaskType::getValue);
	}

	public String getBeginTime_() {
		return Formats.formatDefDate(beginTime);
	}

	public String getEndTime_() {
		return Formats.formatDefDate(endTime);
	}

	public Integer getMaxExp() {
		if (minExp != null && ruleType != null && Cmp.nullToZero(maxLimit) > 1) {
			if (ruleType == UserLevelRuleType.BUY_VIP || ruleType.category == UserLevelRuleCategory.ACTIVE) {
				return minExp * maxLimit;
			}
		}
		return null;
	}

	public String getStatus_() {
		return X.map(statusDescriptor, ActivityStatusDescriptor::getLabel);
	}

	public Integer getStatus() {
		return X.map(statusDescriptor, ActivityStatusDescriptor::getValue);
	}

}
